package com.hula.onion.book.server.spider.platforms.daokoudai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hula.onion.book.server.common.CommonResponse;
import com.hula.onion.book.server.common.LoginForm;
import com.hula.onion.book.server.config.ResponseStatusEnum;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.utils.MoneyUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Description: 道口贷
 * Create Date:2018/1/17 19:49
 *
 * @author shan
 * @version 1.0
 */
@Component
public class DaoKouDaiSpiderHelper {

    /**
     * 组装请求参数
     *
     * @param form
     * @return
     */
    public Map<String, Object> installRequestParams(LoginForm form) {
        Map<String, Object> map = new HashMap<>(2);
        map.put("username", form.getUserName());
        map.put("password", DigestUtils.md5Hex(form.getPassword()));
        return map;
    }

    /**
     * 解析账户余额
     *
     * @param json
     * @return
     */
    public CommonResponse parseAccountBalance(String json) {

        try {
            AccountInfo accountInfo = new AccountInfo();
            JSONObject funInfo = JSON.parseObject(json)
                    .getJSONObject("accountFunds");
            accountInfo.setAmount(funInfo.getBigDecimal("accountAmt"));
            accountInfo.setYesterdayIntrest(funInfo.getBigDecimal("yesterdayProfitAmt"));
            accountInfo.setTotalIntrest(funInfo.getBigDecimal("totalInterestAmt"));
            accountInfo.setWaitIntrest(funInfo.getBigDecimal("toReturnInterest"));
            return new CommonResponse(accountInfo);

        } catch (Exception e) {
            return new CommonResponse(ResponseStatusEnum.QUERY_FAIL);
        }

    }


}
